package fxmedia.ui;

// Import JavaFX Classes
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

// Import Application classes
import fxmedia.Constants;
import fxmedia.FxMedia;

public class OrderButton extends Label {
	public static enum State { CONTINUOUS_PLAY, SHUFFLE_TRACKS };
	private State state = State.CONTINUOUS_PLAY;

	/**
	 * Constructs the track order button.<br/>
	 * @param app The media player application.<br/>
	 */
	public OrderButton(final FxMedia app) {
		super();
		this.setContinuousPlay();
		this.initMouseClickListener(app);
	} // END constructor

	/**
	 * Adds a listener to detect mouse clicks on the play button.<br/>
	 * @param app The media player application.<br/>
	 */
	private void initMouseClickListener(final FxMedia app) {
		this.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent me) {
				if(State.CONTINUOUS_PLAY == state) {
					setShuffleTracks();
				} else if(State.SHUFFLE_TRACKS == state) {
					setContinuousPlay();
				} // END if/else
			} // END handle
		}); // END setOnMouseClicked
	} // END initMouseClickListener

	public State getState() {
		return state;
	} // END getState

	private void setContinuousPlay() {
		state = State.CONTINUOUS_PLAY;
		setGraphic(new ImageView(new Image(FxMedia.class.getResourceAsStream(Constants.PLAY_CONTINUOUS_IMAGE_PATH))));
	} // END setContinuousPlay

	private void setShuffleTracks() {
		state = State.SHUFFLE_TRACKS;
		setGraphic(new ImageView(new Image(FxMedia.class.getResourceAsStream(Constants.PLAY_SHUFFLE_IMAGE_PATH))));
	} // END setShuffleTracks
} // END class OrderButton
